*******		REPLICATION FILES
*******		Climate Variability and Irregular Migration to the European Union 
*******		Global Environmental Change 
*******		Fabien Cottier and Idean Salehyan
*******		Replication: Sensitivity analysis (S4) quarterly spei data (Tables A.15-17, Figures A.12-13)
*******		This version: April 2021

* note: stata packages required for running models
* - coefplot
* - crossfold
* - plottig


clear all

set more off
set scheme plottig
cd "path/to/replication/directory"

* load data
use "data_quarter.dta", clear 

* load stata risk scripts
qui do scripts/script_rr_spei_quarterly_082019

* duplicates data
duplicates list cowc year quarter
duplicates tag cowc year quarter, gen(_dupl)
duplicates drop cowc year quarter, force

* set time series
sort cowc year quarter
gen quarter_int = real(regexs(1)) if regexm(quarter,"([0-9]+)")
gen quarterS=(year-2005)*4+quarter_int
order cowc nationality year quarter quarter_int quarterS
tsset cowc quarterS
 
* gen additional variables
encode continent, gen(contN)
recode contN (5=.)


* generate lag quartely migration variables
by cowc: gen nmigrq_exbalk_1tl = nmigrq_exbalk[_n-1]
by cowc: gen nmigrq_exbalk_2tl = nmigrq_exbalk[_n-2]
by cowc: gen nmigrq_exbalk_3tl = nmigrq_exbalk[_n-3]
by cowc: gen nmigrq_exbalk_4tl = nmigrq_exbalk[_n-4]
by cowc: gen nmigrq_exbalk_ln_1tl = nmigrq_exbalk_ln[_n-1]
by cowc: gen nmigrq_exbalk_ln_2tl = nmigrq_exbalk_ln[_n-2]
by cowc: gen nmigrq_exbalk_ln_3tl = nmigrq_exbalk_ln[_n-3]
by cowc: gen nmigrq_exbalk_ln_4tl = nmigrq_exbalk_ln[_n-4]


* gen dummies variables for sample splitting: low agriculture / high agriculture
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* i.year if migr100_exbalk==1, ///
 fe vce(cluster cowc)

sum agriLab if e(sample) & year==2010, d
scalar agriMed=r(p50)
gen agri_H=0 if e(sample) & year==2010
recode agri_H (0=1) if agriLab-agriMed > 0 & year==2010
by cowc: replace agri_H = agri_H[21] if missing(agri_H)

* generate dummies variable for extreme weather (cut-off 10 percentile / 90 percentile)
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* wmean_speiy /// 
 i.year i.quarter_int if migr100_exbalk==1, ///
 fe vce(cluster cowc)

centile (wmean_speiq) if e(sample), centile (10 90)
gen spei_drought = 0 if wmean_speiq!=.
recode spei_drought (0=1) if wmean_speiq <= r(c_1) 
centile (wmean_speiq) if e(sample), centile (10 90)
gen spei_hrain = 0 if wmean_speiq!=.
recode spei_hrain (0=1) if wmean_speiq >=  r(c_2) 


*******		  Macros

global SPEI_Y0 L(0/3).wmean_speiq

global SPEI_SQ_Y0 c.wmean_speiq##c.wmean_speiq cL1.wmean_speiq##cL1.wmean_speiq ///
	cL2.wmean_speiq##cL2.wmean_speiq cL3.wmean_speiq##cL3.wmean_speiq
 
global SPEI_Y0Y2 L(0/11).wmean_speiq

global SPEI_SQ_Y0Y2 c.wmean_speiq##c.wmean_speiq cL1.wmean_speiq##cL1.wmean_speiq /// 
	cL2.wmean_speiq##cL2.wmean_speiq cL3.wmean_speiq##cL3.wmean_speiq ///
	cL4.wmean_speiq##cL4.wmean_speiq cL5.wmean_speiq##cL5.wmean_speiq /// 
	cL6.wmean_speiq##cL6.wmean_speiq cL7.wmean_speiq##cL7.wmean_speiq ///
    cL8.wmean_speiq##cL8.wmean_speiq cL9.wmean_speiq##cL9.wmean_speiq ///
	cL10.wmean_speiq##cL10.wmean_speiq cL11.wmean_speiq##cL11.wmean_speiq ///


*******		 Table A.15

* NULL Model // No SPEI term
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 /// 
	i.year i.quarter_int if migr100_exbalk==1, ///
	fe vce(cluster cowc)
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_*  /// 
	i.year i.quarter_int if e(sample), ///
	fe vce(cluster cowc)
est sto tabA15_m0
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse null model     : " CVrmse
* list countries included in sample 
est resto tabA15_m0
unique cowc if e(sample)
tabulate nationality contN if e(sample)


* Model 1
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 /// 
	i.year i.quarter_int if migr100_exbalk==1, ///
	fe vce(cluster cowc)
est sto tabA15_m1
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq 
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse
* linear combination of coefficients
est resto tabA15_m1
lincom (L0.wmean_speiq + L1.wmean_speiq + L2.wmean_speiq + L3.wmean_speiq)*-0.75, eform
lincom (L0.wmean_speiq + L1.wmean_speiq + L2.wmean_speiq + L3.wmean_speiq)*0.75, eform


* Model 2
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0 ///
 i.year i.quarter_int if migr100_exbalk==1, ///
 fe vce(cluster cowc)
est sto tabA15_m2
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq cL0.wmean_speiq#cL0.wmean_speiq cL1.wmean_speiq#cL1.wmean_speiq cL2.wmean_speiq#cL2.wmean_speiq cL3.wmean_speiq#cL3.wmean_speiq
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 3
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0Y2 /// 
	i.year i.quarter_int if migr100_exbalk==1, ///
	fe vce(cluster cowc)
est sto tabA15_m3
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq L4.wmean_speiq L5.wmean_speiq L6.wmean_speiq L7.wmean_speiq L8.wmean_speiq L9.wmean_speiq L10.wmean_speiq L11.wmean_speiq
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 4
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0Y2 ///
	i.year i.quarter_int if migr100_exbalk==1, ///
	fe vce(cluster cowc)
est sto tabA15_m4
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq L4.wmean_speiq L5.wmean_speiq L6.wmean_speiq L7.wmean_speiq L8.wmean_speiq L9.wmean_speiq L10.wmean_speiq L11.wmean_speiq cL0.wmean_speiq#cL0.wmean_speiq cL1.wmean_speiq#cL1.wmean_speiq cL2.wmean_speiq#cL2.wmean_speiq cL3.wmean_speiq#cL3.wmean_speiq cL4.wmean_speiq#cL4.wmean_speiq cL5.wmean_speiq#cL5.wmean_speiq cL6.wmean_speiq#cL6.wmean_speiq cL7.wmean_speiq#cL7.wmean_speiq cL8.wmean_speiq#cL8.wmean_speiq cL9.wmean_speiq#cL9.wmean_speiq cL10.wmean_speiq#cL10.wmean_speiq cL11.wmean_speiq#cL11.wmean_speiq
estat ic 
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse




*******		 Table A.16


* NULL Model // agrarian countries
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 ///
	i.year i.quarter_int if migr100_exbalk==1 & agri_H==1, ///
	fe vce(cluster cowc)
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* ///
	i.year i.quarter_int if e(sample), ///
	fe vce(cluster cowc)
est sto tabA16_m0H
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse null model     : " CVrmse
* list of countries
est resto tabA16_m0H
unique cowc if e(sample)
tabulate nationality contN if e(sample)

* NULL Model // non-agrarian countries
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 ///
	i.year i.quarter_int if migr100_exbalk==1 & agri_H==0, ///
	fe vce(cluster cowc)
qui xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* ///
	i.year i.quarter_int if e(sample), ///
	fe vce(cluster cowc)
est sto tabA16_m0L
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse null model     : " CVrmse
global CVrmsenull_LA=round(CVrmse,0.001)
* list of countries
est resto tabA16_m0L
unique cowc if e(sample)
tabulate nationality contN if e(sample)


* Model 5 // agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 ///
  i.year i.quarter_int if migr100_exbalk==1 & agri_H==1, ///
  fe vce(cluster cowc)
est sto tabA16_m5
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq 
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse
* linear combination of coefficients
est resto tabA16_m5
lincom (L0.wmean_speiq + L1.wmean_speiq + L2.wmean_speiq + L3.wmean_speiq)*-0.75, eform
lincom (L0.wmean_speiq + L1.wmean_speiq + L2.wmean_speiq + L3.wmean_speiq)*0.75, eform

* Model 6 // non-agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0 ///
  i.year i.quarter_int  if migr100_exbalk==1 & agri_H==0, ///
  fe vce(cluster cowc)
est sto tabA16_m6
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq 
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse
* linear combination of coefficients
est resto tabA16_m6
lincom (L0.wmean_speiq + L1.wmean_speiq + L2.wmean_speiq + L3.wmean_speiq)*-0.75, eform
lincom (L0.wmean_speiq + L1.wmean_speiq + L2.wmean_speiq + L3.wmean_speiq)*0.75, eform


* Model 7 // agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0 ///
  i.year i.quarter_int  if migr100_exbalk==1 & agri_H==1, ///
  fe vce(cluster cowc)
est sto tabA16_m7
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq cL0.wmean_speiq#cL0.wmean_speiq cL1.wmean_speiq#cL1.wmean_speiq cL2.wmean_speiq#cL2.wmean_speiq cL3.wmean_speiq#cL3.wmean_speiq
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 8 // non-agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0 ///
  i.year i.quarter_int  if migr100_exbalk==1 & agri_H==0, ///
  fe vce(cluster cowc)
est sto tabA16_m8
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq cL0.wmean_speiq#cL0.wmean_speiq cL1.wmean_speiq#cL1.wmean_speiq cL2.wmean_speiq#cL2.wmean_speiq cL3.wmean_speiq#cL3.wmean_speiq
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 9 // agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0Y2 ///
  i.year i.quarter_int  if migr100_exbalk==1 & agri_H==1, ///
  fe vce(cluster cowc)
est sto tabA16_m9
unique cowc if e(sample)
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq L4.wmean_speiq L5.wmean_speiq L6.wmean_speiq L7.wmean_speiq L8.wmean_speiq L9.wmean_speiq L10.wmean_speiq L11.wmean_speiq
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 10 // non-agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_Y0Y2 ///
  i.year i.quarter_int  if migr100_exbalk==1 & agri_H==0, ///
  fe vce(cluster cowc)
est sto tabA16_m10
unique cowc if e(sample)
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq L4.wmean_speiq L5.wmean_speiq L6.wmean_speiq L7.wmean_speiq L8.wmean_speiq L9.wmean_speiq L10.wmean_speiq L11.wmean_speiq
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse


* Model 11 // agrarian countriese
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0Y2 ///
  i.year i.quarter_int  if migr100_exbalk==1 & agri_H==1, ///
  fe vce(cluster cowc)
est sto tabA16_m11
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq L4.wmean_speiq L5.wmean_speiq L6.wmean_speiq L7.wmean_speiq L8.wmean_speiq L9.wmean_speiq L10.wmean_speiq L11.wmean_speiq cL0.wmean_speiq#cL0.wmean_speiq cL1.wmean_speiq#cL1.wmean_speiq cL2.wmean_speiq#cL2.wmean_speiq cL3.wmean_speiq#cL3.wmean_speiq cL4.wmean_speiq#cL4.wmean_speiq cL5.wmean_speiq#cL5.wmean_speiq cL6.wmean_speiq#cL6.wmean_speiq cL7.wmean_speiq#cL7.wmean_speiq cL8.wmean_speiq#cL8.wmean_speiq cL9.wmean_speiq#cL9.wmean_speiq cL10.wmean_speiq#cL10.wmean_speiq cL11.wmean_speiq#cL11.wmean_speiq
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 12 // non-agrarian countries
* low reliance on agriculture
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* $SPEI_SQ_Y0Y2 ///
  i.year i.quarter_int  if migr100_exbalk==1 & agri_H==0, ///
  fe vce(cluster cowc)
est sto tabA16_m12
* F test spei parameters & aic
test L0.wmean_speiq L1.wmean_speiq L2.wmean_speiq L3.wmean_speiq L4.wmean_speiq L5.wmean_speiq L6.wmean_speiq L7.wmean_speiq L8.wmean_speiq L9.wmean_speiq L10.wmean_speiq L11.wmean_speiq cL0.wmean_speiq#cL0.wmean_speiq cL1.wmean_speiq#cL1.wmean_speiq cL2.wmean_speiq#cL2.wmean_speiq cL3.wmean_speiq#cL3.wmean_speiq cL4.wmean_speiq#cL4.wmean_speiq cL5.wmean_speiq#cL5.wmean_speiq cL6.wmean_speiq#cL6.wmean_speiq cL7.wmean_speiq#cL7.wmean_speiq cL8.wmean_speiq#cL8.wmean_speiq cL9.wmean_speiq#cL9.wmean_speiq cL10.wmean_speiq#cL10.wmean_speiq cL11.wmean_speiq#cL11.wmean_speiq
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse



*******		 Table A.17



* Model 13 // agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* L(0/3).spei_drought L(0/3).spei_hrain ///
	  i.year i.quarter_int if migr100_exbalk==1 & agri_H==1, fe vce(cluster cowc)
est sto tabA17_m13
* F test spei parameters & aic
test L0.spei_drought L1.spei_drought L2.spei_drought L3.spei_drought L0.spei_hrain L1.spei_hrain L2.spei_hrain L3.spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse
* linear combination of coefficients
est resto tabA17_m13
lincom _b[spei_drought]+_b[L1.spei_drought]+_b[L2.spei_drought]+_b[L3.spei_drought], eform
lincom _b[spei_hrain]+_b[L1.spei_hrain]+_b[L2.spei_hrain]+_b[L3.spei_hrain], eform

* Model 14 // non-agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* L(0/3).spei_drought L(0/3).spei_hrain ///
  i.year i.quarter_int if migr100_exbalk==1 & agri_H==0, fe vce(cluster cowc)
est sto tabA17_m14
* F test spei parameters & aic
test L0.spei_drought L1.spei_drought L2.spei_drought L3.spei_drought L0.spei_hrain L1.spei_hrain L2.spei_hrain L3.spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse
* linear combination of coefficients
est resto tabA17_m14
lincom _b[spei_drought]+_b[L1.spei_drought]+_b[L2.spei_drought]+_b[L3.spei_drought], eform
lincom _b[spei_hrain]+_b[L1.spei_hrain]+_b[L2.spei_hrain]+_b[L3.spei_hrain], eform


* Model 15 // agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* L(0/11).spei_drought L(0/11).spei_hrain  ///
	  i.year i.quarter_int if migr100_exbalk==1 & agri_H==1, fe vce(cluster cowc)
est sto tabA17_m15
* F test spei parameters & aic
test L0.spei_drought L1.spei_drought L2.spei_drought L3.spei_drought L4.spei_drought L5.spei_drought L6.spei_drought L7.spei_drought L8.spei_drought L9.spei_drought L10.spei_drought L11.spei_drought L0.spei_hrain L1.spei_hrain L2.spei_hrain L3.spei_hrain L4.spei_hrain L5.spei_hrain L6.spei_hrain L7.spei_hrain L8.spei_hrain L9.spei_hrain L10.spei_hrain L11.spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse

* Model 16 // non-agrarian countries
xtreg nmigrq_exbalk_ln nmigrq_exbalk_ln_* L(0/11).spei_drought L(0/11).spei_hrain  ///
  i.year i.quarter_int if migr100_exbalk==1 & agri_H==0, fe vce(cluster cowc)
est sto tabA17_m16
* F test spei parameters & aic
test L0.spei_drought L1.spei_drought L2.spei_drought L3.spei_drought L4.spei_drought L5.spei_drought L6.spei_drought L7.spei_drought L8.spei_drought L9.spei_drought L10.spei_drought L11.spei_drought L0.spei_hrain L1.spei_hrain L2.spei_hrain L3.spei_hrain L4.spei_hrain L5.spei_hrain L6.spei_hrain L7.spei_hrain L8.spei_hrain L9.spei_hrain L10.spei_hrain L11.spei_hrain
estat ic
* cross validation / RMSE
local reg_call=e(cmdline)
qui crossfold `reg_call'
mata : st_numscalar("CVrmse", sqrt(mean((st_matrix("r(est)")):^2)))
di as text   "Avg rmse     : " CVrmse



*******		 Relative risk figures




* Figure A.12 (Model 2, Table A.15)

qui rr_est tabA15_m2 "quadratic"


* Figure A.13 (Model 7 & 8, Table A.16)

qui rr_est_Sp tabA16_m7 tabA16_m8 "quadratic"




*******		 distance to European union


* distance to European Union // agrarian countries
est resto tabA16_m5
sum dist if e(sample)

* distance to European Union // non-agrarian countries
est resto tabA16_m6
sum dist if e(sample)
